package com.beiing.tianshuai.tianshuai.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.ContactsBean;
import com.beiing.tianshuai.tianshuai.util.PinYinUtils;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

/**
 * Created by Jinni on 2017/7/24.
 */

public class MyFriendsAdapter extends BaseAdapter {

    private List<ContactsBean> mData;
    private LayoutInflater inflater;
    private Context mContext;

    public MyFriendsAdapter(Context context, List<ContactsBean> data) {
        this.mData = data;
        inflater = LayoutInflater.from(context);
        mContext = context;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_my_friends, null);
            holder.tvWord = (TextView) convertView.findViewById(R.id.tv_word);
            holder.tvName = (TextView) convertView.findViewById(R.id.tv_friends_name);
            holder.tvUnit = (TextView) convertView.findViewById(R.id.tv_friends_school);
            holder.mUserAvatar = (CircleImageView) convertView.findViewById(R.id.ci_friends_header);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ContactsBean bean = mData.get(position);
        String word = PinYinUtils.getPinyin(String.valueOf(bean.getName())).substring(0, 1);
        holder.tvWord.setText(word);
        holder.tvName.setText(String.valueOf(bean.getName()));
        holder.tvUnit.setText(bean.getUnit());
        RequestOptions requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .override(350, 350);
        Glide.with(mContext.getApplicationContext())
                .load(TextUtils.isEmpty(
                        bean.getAvatar()) ?
                        R.mipmap.default_avatar :
                        HttpRequestConstant.USER_PIC_HEAD + bean.getAvatar())
                .apply(requestOptions)
                .into(holder.mUserAvatar);
        //将相同字母的合并在一起
        if (position == 0) {
            holder.tvWord.setVisibility(View.VISIBLE);
        } else {
            //后一个与前一个对比，判断首字母是否相同，相同则隐藏
            String headerWord = PinYinUtils.getPinyin(String.valueOf(mData.get(position - 1).getName())).substring(0, 1);
            if (word.equals(headerWord)) {
                holder.tvWord.setVisibility(View.GONE);
            } else {
                holder.tvWord.setVisibility(View.VISIBLE);
            }
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView tvWord;
        private TextView tvName;
        private TextView tvUnit;
        private CircleImageView mUserAvatar;
    }
}
